/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.escom.healthlc.security;

import com.escom.healthlc.entidad.MedicoVO;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Carlos
 */
public class SessionListener implements HttpSessionAttributeListener {

    private static final Logger logger = LoggerFactory.getLogger(SessionListener.class);

    @Override
    public void attributeAdded(HttpSessionBindingEvent hsbe) {
//        logger.debug("attributeAdded");
        HttpSession httpSession = hsbe.getSession();
        String nameAttribute = hsbe.getName();
        if (nameAttribute.equals("usuarioActivio")) {
            ServletContext servletContext = httpSession.getServletContext();
            synchronized (servletContext) {
                List<HttpSession> sesionesContext = (List<HttpSession>) servletContext.getAttribute("medicosOnline");
                if (sesionesContext != null) {
                    MedicoVO medicoSession = (MedicoVO) httpSession.getAttribute("usuarioActivio");
                    for (int i = 0; i < sesionesContext.size(); i++) {
//                        logger.debug("sesionesContext.size() {}", sesionesContext.size());
                        HttpSession sesionContext = sesionesContext.get(i);
                        MedicoVO medicoContext = (MedicoVO) sesionContext.getAttribute("usuarioActivio");
                        String idMedicoContext = medicoContext.getIdMedico();
                        String idMedicoSession = medicoSession.getIdMedico();
//                        logger.debug("idMedicoSession {}", idMedicoSession);
//                        logger.debug("idMedicoContext {}", idMedicoContext);
                        if (idMedicoContext != null && idMedicoSession != null && idMedicoContext.equals(idMedicoSession)) {
                            logger.debug("Invalidar session", idMedicoContext);
                            sesionContext.invalidate();
                            sesionesContext.remove(i);
                            sesionesContext.add(httpSession);
                            break;
                        }
                    }
                    servletContext.setAttribute("medicosOnline", sesionesContext);
                } else {
                    logger.debug("First attributeAdded");
                    sesionesContext = new ArrayList<>();
                    sesionesContext.add(httpSession);
                    servletContext.setAttribute("medicosOnline", sesionesContext);
                }
            }
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent hsbe) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent hsbe) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
